package com.gexin.model.weixin;

import java.io.File;
import java.io.UnsupportedEncodingException;
import java.util.List;

import com.gexin.common.AppGlobal;
import com.gexin.common.utils.SQLUtils;
import com.gexin.common.utils.StringUtil;
import com.gexin.model.weixin.base.BaseWeixinAppConfig;
import com.jfinal.kit.PathKit;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.weixin.sdk.api.ApiConfig;
import com.jfinal.wxaapp.WxaConfig;

/**
 * 业务名称:  t_weixin_app_config(微信配置信息表) 
 */
@SuppressWarnings("serial")
public class WeixinAppConfig extends BaseWeixinAppConfig<WeixinAppConfig> {
	public static final WeixinAppConfig dao = new WeixinAppConfig();
	private  WxaConfig  wxaconfig=null;;
	private  ApiConfig  apiconfig=null;;
	private static final String  Cer_path = PathKit.getRootClassPath()+File.separator+"weixin"+File.separator+"cer"+File.separator;
	
	/**
	 * 获取证书的路径
	 * @return  证书的路径 +完整名称
	 */
	public String getKeystoreFilePath() {
		return Cer_path +get("keystore_file_name");
	}

	/**
	 * 获取微信配置信息
	 * @return
	 */
	public synchronized  ApiConfig  getApiConfig(){
		if(apiconfig==null){
			apiconfig  =new ApiConfig();
			apiconfig.setAppId(getAppId());
			apiconfig.setAppSecret(getAppSecret());
			apiconfig.setEncodingAesKey(getEncodingAesKey());
			apiconfig.setToken(getToken());
			apiconfig.setEncryptMessage(getMessageEncrypt());
		}
		return apiconfig;
	}
	
	
	/**
	 * 获取微信配置信息
	 * @return
	 */
	public synchronized  WxaConfig  getWxaConfigs(){
		if(wxaconfig==null){
			wxaconfig  =new WxaConfig();
			wxaconfig.setAppId(getAppId());
			wxaconfig.setAppSecret(getAppSecret());
			wxaconfig.setEncodingAesKey(getEncodingAesKey());
			wxaconfig.setToken(getToken());
			wxaconfig.setMessageEncrypt(getMessageEncrypt());
		}
		return wxaconfig;
	}
	
	/**
	 * 获取基本信息
	 * @return
	 */
	public  String getRegAuthUrlBase(){
		try {
			String redirect=  java.net.URLDecoder.decode(AppGlobal.Host_Domain_Name+"/weixin/auth/base?current_id="+getId(),"GB2312");
			return "https://open.weixin.qq.com/connect/oauth2/authorize?appid="+getAppId()+"&redirect_uri="+redirect+"&response_type=code&scope=snsapi_base&state=info&connect_redirect=1#wechat_redirect";
		} catch (UnsupportedEncodingException e) {
			return null;
		}
	}
	
	/**
	 * 微信授权全部信息 
	 * @return
	 */
	public  String getRegAuthUrl(){
		try {
			String redirect=  java.net.URLDecoder.decode(AppGlobal.Host_Domain_Name+"/weixin/auth/index?current_id="+getId(),"GB2312");
			return "https://open.weixin.qq.com/connect/oauth2/authorize?appid="+getAppId()+"&redirect_uri="+redirect+"&response_type=code&scope=snsapi_userinfo&state=info&connect_redirect=1#wechat_redirect";
		} catch (UnsupportedEncodingException e) {
			return null;
		}
	}
	
	/**
	 * 查询微信配置分页
	 * @param pageNumber
	 * @param pageSize
	 * @param config
	 * @return
	 */
	public Page<WeixinAppConfig> paginate(int pageNumber, int pageSize,WeixinAppConfig config) {
		SQLUtils sql  = new SQLUtils(" from t_weixin_app_config  where 1=1 ");
			if (config!=null) {
				if (StringUtil.isNotBlank(config.getAppTitle())) {
					sql.whereLike("app_title", config.getAppTitle());				
				}
			}
		sql.append(" order by id ");
		return paginate(pageNumber, pageSize, "select * ", sql.toString());
	}
	
	/**
	 * 根据ID查询微信配置信息或查询全部微信配置信息
	 * @param id
	 * @return
	 */
	public List<WeixinAppConfig> findByIdOrGetAll(Integer id) {
		SQLUtils sql=new SQLUtils(" select * from t_weixin_app_config where 1=1 ");
		if (id!=null) {
			sql.whereEquals("id", id);
		}
		return dao.find(sql.toString());
	}
	
}
